package com.scau.wuwei.library_system.core.service;

import com.scau.wuwei.library_system.core.bean.UserInfo;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

@Service
public class PasswordHelper {
    //随机数的代码生成器
    private static RandomNumberGenerator generator;

    static {
        generator = new SecureRandomNumberGenerator();
    }

    /**
     * 获取工具类
     * @return
     */
    public static ByteSource getByteSource(){
        if (generator!=null){
            return  generator.nextBytes();
        }
        return  null;
    }

    /**
     * 默认为MD5，迭代两次
     * @param user
     */
    public void encryptPassword(UserInfo user) {
        user.setSalt(generator.nextBytes().toHex());

        String password = new SimpleHash("MD5",
                user.getPassword(),
                user.getCredentialsSalt(),
                2).toHex();

        user.setPassword(password);
    }

}
